package cn.com.checne.controller.front;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import cn.com.checne.constants.Constants;
import cn.com.checne.dao.FriendLinkInfoDao;
import cn.com.checne.dao.IntroduceInfoDao;
import cn.com.checne.dao.NewsInfoDao;
import cn.com.checne.dao.PerformanceInfoDao;
import cn.com.checne.model.domain.FriendLinkInfo;
import cn.com.checne.model.domain.IntroduceInfo;
import cn.com.checne.model.domain.NewsInfo;
import cn.com.checne.model.domain.PerformanceInfo;
import cn.com.checne.model.dto.IntroduceQuery;
import cn.com.checne.model.dto.NewsQuery;
import cn.com.checne.model.dto.PerformanceQuery;
import cn.com.checne.util.HeaderUtils;
import cn.com.checne.util.LanguageUtils;

@Controller
public class IndexFrontController {
	protected static final Logger logger = Logger.getLogger(IndexFrontController.class);
	
	@Resource
	private NewsInfoDao newsDao ;
	@Resource
	private PerformanceInfoDao performanceDao ;
	@Resource
	private IntroduceInfoDao introduceDao ;
	@Resource
	private FriendLinkInfoDao linkDao ;
	
	@RequestMapping(value="/{lg}/",method=RequestMethod.GET)
	public ModelAndView index(
			@PathVariable String lg,
			HttpServletRequest request, HttpServletResponse response,ModelMap modelMap) throws Exception {
		
		ModelAndView mav = new ModelAndView();
		lg = StringUtils.isBlank(lg) ? Constants.LANGUAGE_CN : lg ;
		String viewName = "front/" + lg + "/index" ;
		int languageId = LanguageUtils.getLanguage(lg);
		
		
		NewsQuery newsQuery = new NewsQuery();
		//公司新闻-图片新闻
      	NewsInfo newsInfo = new NewsInfo();
		newsQuery = new NewsQuery();
      	newsQuery.setLanguageId(languageId);
      	if(languageId == Constants.language_CN_){
      		newsQuery.setCateId(1);
      	}
      	if(languageId == Constants.LANGUAGE_EN_){
      		newsQuery.setCateId(5);
      	}
      	
      	newsQuery.setIsShowIndex(1);
      	List<NewsInfo> gongsiImageList = newsDao.getNewsImagesListByCondition(newsQuery,0,1);
      	if(gongsiImageList != null && gongsiImageList.size() > 0){
      		newsInfo = gongsiImageList.get(0);
      	}
		//公司新闻-文字新闻
		newsQuery = new NewsQuery();
      	newsQuery.setLanguageId(languageId);
      	if(languageId == Constants.language_CN_){
      		newsQuery.setCateId(1);
      	}
      	if(languageId == Constants.LANGUAGE_EN_){
      		newsQuery.setCateId(5);
      	}
      	newsQuery.setIsShowIndex(1);
      	List<NewsInfo> gongsiNewsList = newsDao.getNewsListByCondition(newsQuery,0,5);
      	
      	//公司业绩
      	PerformanceQuery performanceQuery = new PerformanceQuery();
        performanceQuery.setLanguageId(languageId);
        performanceQuery.setCateId(-1);
        performanceQuery.setIsShowIndex(1);
        performanceQuery.setFirstImage("image");
        performanceQuery.setOrder(1);
        int gsyj = 5 ;
        if(StringUtils.equalsIgnoreCase(lg, Constants.LANGUAGE_CN)){
        	gsyj = 7 ;
        }
        List<PerformanceInfo> yejiList = performanceDao.getPerformanceListByCondition(performanceQuery,0,gsyj);
        String performanceImage = "" ;
        if(yejiList != null && yejiList.size() > 0 ){
        	performanceImage = yejiList.get(0).getFirstImage();
        }
        
        //集团新闻
        newsQuery = new NewsQuery();
        newsQuery.setLanguageId(languageId);
        int  jtxw = 5 ;
      	if(languageId == Constants.language_CN_){
            newsQuery.setCateId(2);
            jtxw = 7 ;
      	}
      	if(languageId == Constants.LANGUAGE_EN_){
            newsQuery.setCateId(6);
      	}
        newsQuery.setIsShowIndex(1);
        List<NewsInfo> jituanList = newsDao.getNewsListByCondition(newsQuery,0,jtxw);
        
        //经营模式
        String jingyingContent = "" ;
        IntroduceQuery introduceQuery = new IntroduceQuery();
        introduceQuery.setLanguageId(languageId);
      	if(languageId == Constants.language_CN_){
            introduceQuery.setCateId(4);
      	}
      	if(languageId == Constants.LANGUAGE_EN_){
            introduceQuery.setCateId(17);
      	}
        introduceQuery.setIsShowIndex(1);
        List<IntroduceInfo> jingyingList = introduceDao.getIntroduceListByCondition(introduceQuery,0,1);
        if(jingyingList != null && jingyingList.size() > 0){
        	IntroduceInfo jingying = jingyingList.get(0);
//        	jingyingContent = StringTools.substring(jingying.getContent(),55,"...");
        	jingyingContent = jingying.getContent() ;
        }
        
        // 友情链接
        List<FriendLinkInfo> companyLinkList = linkDao.getAll(languageId,1);
        List<FriendLinkInfo> otherLinkList = linkDao.getAll(languageId,2);

      	modelMap.put("gongsiNewsImage", newsInfo);
      	modelMap.put("gongsiNewsList", gongsiNewsList);
      	modelMap.put("yejiList", yejiList);
      	modelMap.put("performanceImage", performanceImage);
      	modelMap.put("jituanList", jituanList);
      	modelMap.put("jingyingContent", jingyingContent);
      	modelMap.put("companyLinkList", companyLinkList);
      	modelMap.put("otherLinkList", otherLinkList);

		modelMap.addAllAttributes(LanguageUtils.getModelMap(lg));
		modelMap.addAllAttributes(HeaderUtils.getHeaderMap(languageId));
		mav.setViewName(viewName);
		mav.addAllObjects(modelMap);
		return mav ;
	}
}